package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.SecurityDeviceInfo;
import com.example.prison.service.SecurityDeviceInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "安防巡更设备管理")
@RestController
@RequestMapping("/api")
public class SecurityDeviceInfoController {

    @Autowired
    private SecurityDeviceInfoService securityDeviceInfoService;

//    @Autowired
//    private SystemLogService systemLogService;

    @ApiOperation("获取安防巡更设备列表")
    @GetMapping("/getSecurityDevice")
    public Response<PageInfo<SecurityDeviceInfo>> getSecurityDevice(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                    @Parameter(description = "设备名称")@RequestParam String deviceName,
                                                                    @Parameter(description = "设备编号")@RequestParam String deviceNumber) {
        PageInfo<SecurityDeviceInfo> pageInfo = securityDeviceInfoService.get(pageNumber, pageSize, deviceName, deviceNumber);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增安防巡更设备")
    @PostMapping("/addSecurityDevice")
    public Response<?> addSecurityDevice(@RequestBody SecurityDeviceInfo seDevice) {
        int result = securityDeviceInfoService.add(seDevice);
        if (result == -1) {
            return Response.error("设备已存在！");
        }
        log.info("新增设备"+", 数据："+ JSON.toJSONString(seDevice));
//        systemLogService.addLog("新增设备");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除安防巡更设备")
    @PostMapping("/deleteSecurityDevice")
    public Response<?> deleteSecurityDevice(@Parameter(description = "设备ID") @RequestBody Integer[] ids) {
        securityDeviceInfoService.delete(ids);
        log.info("删除安防巡更设备");
//        systemLogService.addLog("删除安防巡更设备");
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑安防巡更设备")
    @PostMapping("/updateSecurityDevice")
    public Response<?> updateSecurityDevice(@RequestBody SecurityDeviceInfo seDevice) {
        Integer id = seDevice.getDeviceId();
        int result = securityDeviceInfoService.update(seDevice, id);
        if (result == -1) {
            return Response.error("安防巡更设备已存在！");
        }
        log.info("编辑安防巡更设备"+", 数据："+JSON.toJSONString(seDevice));
//        systemLogService.addLog("编辑安防巡更设备");
        return Response.success("更新成功！");
    }

    @ApiOperation("依据id获取安防巡更设备信息")
    @GetMapping("/getSecurityDeviceById")
    public Response<SecurityDeviceInfo> getSecurityDeviceById(@Parameter(description = "设备ID")@RequestParam Integer deviceId) {
        SecurityDeviceInfo securityDevice = securityDeviceInfoService.getById(deviceId);
        if(ObjectUtils.isEmpty(securityDevice)) {
            return Response.error("此设备id不存在！");
        }
        return Response.success("获取成功！", securityDevice);
    }

//    @ApiOperation("依据设备名称获取安防巡更设备信息")
//    @GetMapping("/getSecurityDeviceByName")
//    public Response<PageInfo<SecurityDeviceInfo>> getSecurityDeviceByName(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                              @Parameter(description = "设备名称")@RequestParam String deviceName) {
//        PageInfo<SecurityDeviceInfo> pageInfo = securityDeviceInfoService.getByName(pageNumber, pageSize, deviceName);
//        if(ObjectUtils.isEmpty(pageInfo.getList())) {
//            return Response.error("此安防巡更设备不存在！");
//        }
//        return Response.success("获取成功！", pageInfo);
//    }
//
//    @ApiOperation("依据设备编号获取安防巡更设备信息")
//    @GetMapping("/getSecurityDeviceByNumber")
//    public Response<PageInfo<SecurityDeviceInfo>> getSecurityDeviceByNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
//                                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
//                                                                                @Parameter(description = "设备编号")@RequestParam String deviceNumber) {
//        PageInfo<SecurityDeviceInfo> pageInfo = securityDeviceInfoService.getByNumber(pageNumber, pageSize, deviceNumber);
//        if(ObjectUtils.isEmpty(pageInfo.getList())) {
//            return Response.error("此安防巡更设备不存在！");
//        }
//        return Response.success("获取成功！", pageInfo);
//    }

    @ApiOperation("获取安防巡更设备实时状态")
    @GetMapping("/getSecurityDeviceStatus")
    public Response<PageInfo<SecurityDeviceInfo>> getSecurityDeviceStatus(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        PageInfo<SecurityDeviceInfo> pageInfo = securityDeviceInfoService.getStatus(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

}
